import {
	getVersion
} from '@/static/js/home';
import { completeTask } from '@/static/js/points.js';
import {
	logout
} from '@/static/js/user';
export default {
	data() {
		return {
			version: 0, //本地版本号
			outShow: false,
			userInfo: '',
			settingsList: [{
				name: '账号安全',
				url: '/pages/my/settings/safetyCenter',
				isShow: true
			}, {
				name: '关于我们',
				url: '/pages/my/settings/about?type=about_us',
				isShow: true
			}, {
				name: '用户协议',
				url: '/pages/login/agreements?type=ios_user_agreement&name=用户协议',
				isShow: true
			}, {
				name: '隐私协议',
				url: '/pages/login/agreements?type=ios_privacy_agreement&name=隐私协议',
				isShow: true
			} ,{
				name: '会员协议',
				url: '/pages/my/vip/agreement',
				isShow: true
			} , {
				name: '问答协议',
				url: '/pages/login/agreements?type=wenda_agreement&name=问答协议',
				isShow: true
			},{
				name: '',
				url: '',
				isShow: true
			},],

			showPointsTips: false, //任务完成提示
			pointsObj: {}, //任务完成对象
		};
	},
	methods: {
		//菜单点击
		onClick(item) {
			if (item.url == '') {
				this.checkVersion()
			} else {
				uni.navigateTo({
					url: item.url
				})
			}
		},
		//检查版本号
		checkVersion() {
			//获取本地的版本号
			getVersion({}).then(({
				code,
				data,
				msg
			}) => {
				if (code == 1) {
					let netVersionCode = parseInt(data.versioncode)
					if (this.version >= netVersionCode) {
						this.$tools.msg('当前已是最新版本')
					} else {
						this.$tools.msg('检测到最新版本,正在前往应用市场更新');
						this.completeQuest('C1');
						setTimeout(() => {
							let platform = uni.getSystemInfoSync().platform
							if (platform == 'ios') {
								plus.runtime.openURL(
									"https://apps.apple.com/cn/app/%E5%B0%8F%E7%AB%B9%E8%B4%A2%E7%A8%8E/id6473877777"
								)
							}
						}, 2000)
					}
				}
			})
		},
		//退出
		quit() {
			let that = this
			logout({}).then(res => {
				try {


					this.outShow = false
					this.$tools.msg('已退出')
					uni.removeStorageSync('token')
					uni.removeStorageSync('userInfo')
					this.$store.commit('RESET_STATE');
					uni.removeTabBarBadge({
						index: 3
					})
					//断开连接
					//	#ifdef APP-PLUS
					that.$goeasy.disconnect({
						onSuccess: function () {
							console.log("GoEasy disconnect successfully.")
						},
						onFailed: function (error) {
							console.log("Failed to disconnect GoEasy, code:" + error.code +
								",error:" + error
									.content);
						}
					});
					// #endif
					setTimeout(() => {
						uni.reLaunch({
							url: '/pages/index/index'
						})
					}, 1000)
				} catch (error) {
					console.log('setting', error);
				}
			})
		},

		//完成任务
		completeQuest(code) {
			completeTask({ code }).then(res => {
				if (res.code == 1) {
					this.showPointsTips = true;
					this.pointsObj = res.data;
				}
			})
			setTimeout(() => {
				this.showPointsTips = false;
			}, 3000);
		},
	},
	onLoad(e) {
		var that = this
		uni.getSystemInfo({
			success: function (res) {
				console.log(res)
				that.version = res.appVersionCode
				that.settingsList[that.settingsList.length - 1].name = '版本' + res.appVersion
			}
		});
	},
}